//
//  NewFeatureViewController.swift
//  Weibo007
//
//  Created by 闫崇 on 15/12/16.
//  Copyright © 2015年 闫崇. All rights reserved.
//

import UIKit
import FFAutoLayout
private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {

    
    //界面布局
    let layout = UICollectionViewFlowLayout()
    init() {
        
        super.init(collectionViewLayout: layout)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 注册可重用 Cell
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

      
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        print(self.collectionView?.frame)
        //设置布局
        layout.itemSize = view.bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        //设置分页
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        
        
        
    }
    


    //图片总数
    let imageCount = 4

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        cell.backgroundColor = UIColor.redColor()
    
        cell.index = indexPath.item
        
        
        return cell
    }
    
    /**
     cell 展示完成后调用
     
     :param: collectionView  collection
     :param: cell           之前显示的 Cell
     :param: indexPath       之前的 indexPath
     */
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //获取当前显示的 Cell
        let path = collectionView.indexPathsForVisibleItems().last!
        
        if path.item == imageCount - 1{
            
            (collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell).showStartBtn()
        }
            
    }

}



class NewFeatureCell : UICollectionViewCell {
    
    
    //图像索引
    var index : Int = 0 {
      
        didSet{
            
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            startBtn.hidden = true
        }
        
    }
    
    
    func showStartBtn() {
        
        //动画
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        startBtn.hidden = false
        startBtn.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                self.startBtn.userInteractionEnabled = true
        }
        
    }
    
    //按钮的点击事件
    func startBtnClickt() {
        // object = true 表示显示 MainViewController
        NSNotificationCenter.defaultCenter().postNotificationName(YCSwitchRootViewController, object: true)
        
    }
    
    
    
    //MARK - 懒加载
    //图像
    lazy var iconView : UIImageView = {
  
       return UIImageView()
    }()
    
    //按钮
    lazy var startBtn : UIButton = {
       
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        //添加点击事件
        btn.addTarget(self, action: "startBtnClickt", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        btn.sizeToFit()
        
        return btn
        
    }()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(iconView)
        addSubview(startBtn)
        
        //自动布局一定要创建完控件就添加
        startBtn.ff_AlignInner(ff_AlignType.BottomCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -160))
       
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        iconView.frame = self.bounds
    }
    
    
    
}



